<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;

class AdminController extends Controller
{
    //图片列表
    public function index(Request $request)
    {
    	$data = DB::table('image') -> orderBy('imagedate','desc') -> paginate(10);
    	foreach ($data as $k => $v) {
    		$v -> uname = DB::table('users') -> where('id',$v -> userid) -> value('name');
    	}
    	
    	return view('admin.ace.active',['data' => $data,'request' => $request -> all()]);
    }

    //图片申请通过
    public function edit($id)
    {
    	$state = DB::table('image') -> where('id',$id) ->value('state');
    	if($state == '0')
    	{
    		$res = DB::table('image') -> where('id',$id) -> update(['state' => '1']);
    	}else{
    		$res = DB::table('image') -> where('id',$id) -> update(['state' => '0']);
    	}
    	
    	if($res)
    	{
    		return back() -> with(['info' => '操作成功']);
    	}else{
    		return back() -> with(['info' => '操作失败']);
    	}
    }

    //图片删除
    public function del($id)
    {
    	$res = DB::table('image') -> where('state','0') -> delete($id);
    	if($res)
    	{
    		return back() -> with(['info' => '删除成功']);
    	}else{
    		return back() -> with(['info' => '删除失败,请查看图片是否已通过']);
    	}
    }

    //活动列表
    public function act(Request $request)
    {
        $data = DB::table('active') -> orderBy('active_out','desc') -> paginate(10);
        foreach ($data as $k => $v) {
            $v -> uname = DB::table('admin') -> where('id',$v -> activeadminid) -> value('name');
        }
        return view('admin.ace.photo',['data' => $data,'request' => $request -> all()]);
    }

    //删除
    public function Pdel($id)
    {
        $res = DB::table('active') -> where('state','0') -> orwhere('state','2') ->delete($id);
        if($res)
        {
            return back() -> with(['info' => '删除成功']);
        }else{
            return back() -> with(['info' => '删除失败,请查看活动是否已开始']);
        }
    }

    //活动提前结束
    public function Pedit($id)
    {
        $state = DB::table('active') -> where('id',$id) -> value('state');
        if($state == '0')
        {
            return back() -> with(['info' => '活动未开始']);
        }elseif($state == '2'){
            return back() -> with(['info' => '活动已结束']);
        }

        $res = DB::table('active') -> where('id',$id) -> update(['state' => '2']);
            
        if($res)
        {
            return back() -> with(['info' => '操作成功']);
        }else{
            return back() -> with(['info' => '操作失败']);
        }
    }

    //活动添加页面
    public function addActive ()
    {
        return view('admin/ace/addActive');
    }
    //活动添加动作
    public function add(Request $request)
    {
        $this -> validate($request,[
            'activename' => 'required',
            'time' => 'required|min:1|max:30',
            ],[
            'activename.required' => '活动名不能为空',
            'time.required' => '持续时间不能为空',
            'time.min' => '持续时间最少为1天',
            'time.max' => '持续时间最多为30天',
            ]);
        $data = $request -> except('_token');
        if($data['time'] < 1)
        {
            return back() -> with(['info' => '活动不得少于一天']);
        }
        //获取用户id
        $id = session('admin') -> id;

        //获取当前时间
        $time = time();
        $day = '86400'*$data['time'];
        //获取活动结束时间
        $outTime = $time+$day; 
        $outTime = date('Y-m-d H:i:s',$outTime);
        $time = date('Y-m-d H:i:s',$time);

        $date = [];
        $date['activename'] = $data['activename'];
        $date['text'] = $data['text'];
        $date['activeadminid'] = $id;
        $date['active_at'] = $time;
        $date['active_out'] = $outTime;
        $date['state'] = '1';

        $res = DB::table('active') -> insertGetId($date);
        if($res)
        {
            return back() -> with(['info' => '添加成功']);
        }else{
            return back() -> with(['info' => '添加失败']);
        }

    }
}
